package collaborativefiltering4plsusers;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class FindIMSIStraniero {

	public static void main (String[] args) throws Exception{
		Map<String , List<Event>> map = new HashMap<String, List<Event>>();
		 File Folder = new File("C:\\Users\\Alket\\junocode\\FlickrAPI\\AllData");
	        File files[];
	        files = Folder.listFiles();
	       
        List<Event> eventlist = new ArrayList<Event>();
	        if(files.length>1)
	        {
	        	
	            for(int i = 0;i<files.length; i++){
	            
	            	String filename = files[i].getName();
	            	//String filename2 = filename.substring(filename.indexOf("PLS"),
	            		//	filename.indexOf("."));
	            	System.out.println("leggo file "+i+" in cerca di imsi stranieri");
	            	BufferedReader  br = new BufferedReader(new FileReader(files[i]));
	            	
	                String line = "";	
	                while((line = br.readLine())!=null){
	                	String [] riga = line.split("\t");
	                	String username = riga[0];
	                	int imsi = Integer.parseInt(riga[1]);
	                	long celllac = Long.parseLong(riga[2]);
	                	long tempo = Long.parseLong(riga[3]);
	                	double lat = Double.parseDouble(riga[4]);
	                	double lon = Double.parseDouble(riga[5]);
	                	int raggio = Integer.parseInt(riga[6]);
	                	if((imsi != 22201) && (imsi != 22210)&& (imsi != 22299)
	                			&& (imsi != 22288)&& (imsi != 22230)&& (imsi != 22207)){
	                	eventlist.add(new Event(username, imsi, celllac, tempo, lat, lon, raggio));
	                
	                	}
	                }
	                br.close();
	                
	                if((i+1) % 942 ==0){
	                	 Collections.sort(eventlist);
	                 	printList(eventlist, "TuttiImsiStranieri_final"+i+".csv");
	                 	eventlist = new ArrayList<Event>();
	                }
	                	
	                }
	               
	            }
	        System.out.println(eventlist.size());
	        }
	
	
	public static void printList(List<Event> list, String file) throws Exception{
		
		PrintWriter out = new PrintWriter(new FileWriter(new File(file)));
		for (int i = 0; i < list.size(); i++) {
			out.print(list.get(i).usr+",");
			out.print(list.get(i).imsi+",");
			out.print(list.get(i).cellac+",");
			out.print(list.get(i).ts+",");
			out.print(list.get(i).lat+",");
			out.print(list.get(i).lon+",");
			out.println(list.get(i).raggio);
		}
		out.close();
	}
	
     
}